#include "delegate.h"
#include <QLineEdit>
#include <iostream>
#include "filewidget.h"
delegate::delegate(QObject *parent) : QItemDelegate(parent){
}

QWidget *delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &/*index*/) const{
    QLineEdit *editor = new QLineEdit(parent);
    editor->setMaxLength(2);
    editor->setInputMask("HH");
    return editor;
}

void delegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
    QString value = index.model()->data(index,Qt::EditRole).toString();

    QLineEdit *lineEdit = static_cast<QLineEdit *>(editor);
    lineEdit->setText(value);
}

void delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
    QLineEdit *lineEdit = static_cast<QLineEdit *>(editor);
    QString value = lineEdit->text();
    if( value.size()<2 ){
        //std::cout<<"Inappropriate data format, byte value must contain two hex values."<<std::endl;
    }
    else{
        model->setData(index, value, Qt::EditRole);
        static_cast<fileWidget *>(this->parent())->updateTemplateWidget();
    }
}

void delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const{
    editor->setGeometry(option.rect);
}
